package com.wh.wisdomsite.xbox.client.service.publicApi.pr;

import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.de.entity.Device;
import com.wh.wisdomsite.xbox.de.service.IDeviceService;
import com.wh.wisdomsite.xbox.pojo.publicApi.pr.PubApiProjectIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 公共API 项目 Service
 * @author 丁奕
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.client.service.publicApi.pr
 * @date 2018年12月11日 10:30
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@ServiceProvider()
public class PublicApiProjectService {

    private static final Log logger = LogFactory.getLog(PublicApiProjectService.class);

    @Autowired
    private IProjectService iProjectService;

    @Autowired
    private IDeviceService iDeviceService;

    /**
     * 根据设备ID查询项目代码
     * @param in
     * @return
     */
    @ServiceMethod(method = "zhgd.publicApi.pr.getPrCodeByDeviceId", title = "获取项目代码", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getPrCodeByDeviceId(PubApiProjectIn in){
        logger.info("获取项目代码->>zhgd.publicApi.pr.getProjectList->>开始");
        CommonResult commonResult = new CommonResult();
        try{
            if(StringUtils.isEmpty(in.getDeviceId())){
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGD_PUBAPI_PROJECT_DEVICEID_NOT_EMPTY);
                return commonResult;
            }
            //获取设备列表
            List<Device> devices = iDeviceService.getDeviceById(in.getDeviceId());
            List<String> prCodeList = new ArrayList<>();
            //遍历设备列表，将对应的项目的项目代码放入list
            for (Device device : devices){
                Project project = iProjectService.selectById(device.getPrId());
                prCodeList.add(project.getPrCode());
            }
            HashSet hashSet = new HashSet(prCodeList);
            prCodeList.clear();
            prCodeList.addAll(hashSet);
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
            commonResult.setResult(prCodeList);
            logger.info("获取项目代码->>zhgd.publicApi.pr.getProjectList->>结束");
        }catch (Exception e){
            logger.error("获取项目代码->>zhgd.publicApi.pr.getProjectList->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }
}
